<template>
  <div class="config-sample-collection">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="0">
            <a-col :md="6" :sm="24">
              <a-form-item label="网关名称">
                <a-select
                  :showSearch="true"
                  optionFilterProp="children"
                  style="width: 100%"
                  v-model="queryParam.gatewayId"
                  allow-clear
                  placeholder="请选择网关名称"
                >
                  <a-select-option
                    v-for="(item, ind) in gatewayList"
                    :key="ind"
                    :value="item.id">{{ item.gatewayName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="10" :sm="24">
              <span
                class="table-page-search-submitButtons"
                style="float: left;padding-left: 16px;"
              >
                <a-button
                  type="primary"
                  class="btn_search_reset"
                  @click="$refs.table.refresh()"><a-icon type="search"/>查询</a-button>
                <a-button
                  style="margin-left: 8px"
                  class="btn_search_reset"
                  @click="
                    () => {
                      queryParam = { deviceIds: deviceIds.toString(), gatewayIds: gatewayIds.toString() }
                      $refs.table.refresh()
                    }"><a-icon type="reload"/>重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        id="table-wrapper"
        ref="table"
        size="middle"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
      >
        <span
          slot="fileId"
          slot-scope="text, record">
          <a @click="$refs.playForm.play(record)">{{ text === null || text === '' ? '无' : '播放' }}</a>
        </span>
        <span
          slot="action"
          slot-scope="text, record">
          <a @click="$refs.detailDlg.init(record)" class="btn_edit">详情</a>
        </span>
      </s-table>

      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
      <detail-dlg ref="detailDlg"/>

    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { page, del } from '@/api/modular/main/bizcollectionsamplemanage/bizCollectionSampleManageManage'
  import addForm from '../bizcollectionsamplemanage/addForm'
  import editForm from '../bizcollectionsamplemanage/editForm'
  import detailDlg from '../bizcollectionsamplemanage/detailDlg'

  export default {
    components: {
      STable,
      addForm,
      editForm,
      detailDlg
    },
    props: {
      stationId: {
        type: Number,
        require: false
      }
    },
    data () {
      return {
        data: [],
        page: {},
        loading: false,
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        queryParam1: {}, // 查询参数
        columns: [
          // 表头
          {
            title: '网关名称',
            dataIndex: 'gatewayName'
          },
          {
            title: '通道',
            dataIndex: 'gatewayChannel',
            width: 50
          },
          {
            title: '时间',
            dataIndex: 'createAt',
            width: 180,
            align: 'center'
          },
          {
            title: '文件数量',
            dataIndex: 'fileNumber',
            with: 50,
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            width: 50,
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        gatewayList: [],
        gatewayIds: [],
        deviceIds: [],
        stationTreeIndex: -1,
        // 通道号
        channel: ''
      }
    },
    methods: {
      del (record) {
        del(record)
          .then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('删除失败：' + res.message)
            }
          })
          .catch((err) => {
            this.$message.error('删除错误：' + err.message)
          })
      },
      handleTaskOk (record) {
        // this.$refs.taskProgressDlg.init(record, 1)
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      getGatewayList (stationId) {
        gatewayList({ stationId: stationId }).then((res) => {
          if (res.success) {
            this.gatewayList = res.data
            this.gatewayIds = []
            this.gatewayList.filter((value) => {
              this.gatewayIds.push(value.id)
            })
            this.queryParam.gatewayIds = this.gatewayIds.toString()
            this.handleOk()
          }
        })
      }
    },
    created () {
      this.queryParam.stationId = this.stationId
      const param = {
        stationId: this.stationId
      }
      gatewayList(param).then(res => {
        this.gatewayList = res.data
        for (let i = 0; i < res.data.length; i++) {
          this.gatewayIds.push(res.data[i].id)
        }
        this.queryParam.gatewayIds = this.gatewayIds.toString()
        try {
          this.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      })
    }
  }
</script>

<style lang="less" scoped>
  @import '../~@/assets/css/list.less';

  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
